﻿using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class TalkText : MonoBehaviour
{
    [SerializeField] private TextMeshProUGUI textGUI;
    [SerializeField] private float Text_appearInterval=0.1f;
    [SerializeField] private float TextBox_appearSpeed=1f;

    [SerializeField] private GameObject TalkBox;
    [SerializeField] private CanvasGroup canvasGroup;

    private string text;
    

    private bool isShow = false;
    private int nowCount = 0;

    private bool notEnd = true;
    private bool appearing=false;
    private bool disappearing=false;

    private float text_waitTime=0;
    private bool isFirst=true;
    private bool isFirstAppear=false;
    private void Start()
    {
        text = textGUI.text;
        textGUI.text = "";
        canvasGroup.alpha = 0;
    }

    private void Update()
    {
        if (((!appearing&&!disappearing)||notEnd)&&isShow)
        {
            TextAppear();
        }
        
        if (disappearing && !notEnd&&!isFirstAppear)
        {
            canvasGroup.alpha -= Time.deltaTime * TextBox_appearSpeed;
            if (canvasGroup.alpha <=0 )
            {
                canvasGroup.alpha = 0;
                disappearing = false;
                isShow = false;
            }
        }
        if (appearing || isFirstAppear)
        {
            canvasGroup.alpha += Time.deltaTime * TextBox_appearSpeed;
            if (canvasGroup.alpha >= 1)
            {
                canvasGroup.alpha = 1;
                appearing = false;
                isShow = true;
                isFirstAppear = false; ;
            }
        }
    }


    private void OnTriggerEnter2D(Collider2D collision)
    {
        
        if (collision.CompareTag(TagAndLayersConfigs.Tag.Player))
        {
            nowCount++;
            if (!isShow && !appearing)
            {
                disappearing = false;
                appearing = true;
            }
            if (nowCount>=2)
            {
                nowCount = 2;
            }
            if (isFirst)
            {
                isFirstAppear = true;
                isFirst = false;
            }
        }

    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        
        if (collision.CompareTag(TagAndLayersConfigs.Tag.Player))
        {
            nowCount--;
            if (nowCount <= 0)
            {
                nowCount = 0;
                appearing = false;
                disappearing = true;
            }
        }
        
    }

    private void TextAppear()
    {
        if (notEnd)
        {
            if (!TalkBox.activeSelf)
            {
                return;
            }
            text_waitTime += Time.deltaTime;
            if (text_waitTime >= Text_appearInterval)
            {
                text_waitTime = 0;
                textGUI.text += text.Substring(0, 1);
                if (text.Length != 1)
                {
                    text = text.Substring(1, text.Length - 1);
                }
                else
                {
                    notEnd = false;
                }

            }
        }
    }

}
